package com.suarte.core;

import com.suarte.utils.Persistent;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import org.appfuse.model.BaseObject;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import org.appfuse.model.User;

/**
 * @date Dec 30, 2010
 * @author Ggutierrez
 */
@Entity
@Table(name = "sa_quotations")
public class Quotation extends BaseObject implements Persistent {

    private Long id;
    private Date transDate;
    private String description;
    private Company company;
    private Contact contact;
    private Currency currency;
    private Float subtotal = 0f;
    private Float tax = 0f;
    private Float totalCost = 0f;
    private DeliveryTime deliveryTime;
    private PaymentMethod paymentMethod;
    private Guarantee guarantee;
    private List<QuotationDetail> details = new ArrayList();
    private User createdBy;
    private Date createdOn = new Date();
    private User modifiedBy;
    private Date modifiedOn = new Date();
    private QuotationStatus status = QuotationStatus.SOLICITADA;
    private QuotationStatus deliveryStatus = QuotationStatus.SOLICITADA;
    private Currency exchangeCurrency;
    private Float exchangeRate;
    private Float equivalentAmount;
    private Float localAmount;
    private Float foreignAmount;
    private Long oldId;
    private String notes;
    private Resolution resolution;
    private WorkType workType;
    private Long oldStatus;

    public Quotation() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "QUOTATION_SEQ")
    @SequenceGenerator(name = "QUOTATION_SEQ", sequenceName = "QUOTATION_SEQ", allocationSize = 1)
    public Long getId() {
        return id;
    }

    @Column(name = "trans_date")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getTransDate() {
        return transDate;
    }

    @Column(name = "description", length = 250)
    public String getDescription() {
        return description;
    }

    @ManyToOne
    @JoinColumn(name = "company_id")
    public Company getCompany() {
        return company;
    }

    @ManyToOne
    @JoinColumn(name = "contact_id")
    public Contact getContact() {
        return contact;
    }

    @ManyToOne
    @JoinColumn(name = "currency_id")
    public Currency getCurrency() {
        return currency;
    }

    @Column(name = "subtotal")
    public Float getSubtotal() {
        return subtotal;
    }

    @Column(name = "tax")
    public Float getTax() {
        return tax;
    }

    @Column(name = "total_cost")
    public Float getTotalCost() {
        return totalCost;
    }

    @ManyToOne
    @JoinColumn(name = "delivery_time_id")
    public DeliveryTime getDeliveryTime() {
        return deliveryTime;
    }

    @ManyToOne
    @JoinColumn(name = "payment_method_id")
    public PaymentMethod getPaymentMethod() {
        return paymentMethod;
    }

    @ManyToOne
    @JoinColumn(name = "guarantee_id")
    public Guarantee getGuarantee() {
        return guarantee;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    public QuotationStatus getStatus() {
        return status;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "delivery_status")
    public QuotationStatus getDeliveryStatus() {
        return deliveryStatus;
    }

    @ManyToOne
    @JoinColumn(name = "exchange_currency_id")
    public Currency getExchangeCurrency() {
        return exchangeCurrency;
    }

    @OneToMany(mappedBy = "quotation", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    public List<QuotationDetail> getDetails() {
        return details;
    }

    @ManyToOne
    @JoinColumn(name = "created_by")
    public User getCreatedBy() {
        return createdBy;
    }

    @ManyToOne
    @JoinColumn(name = "modified_by")
    public User getModifiedBy() {
        return modifiedBy;
    }

    @Column(name = "created_on")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getCreatedOn() {
        return createdOn;
    }

    @Column(name = "modified_on")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getModifiedOn() {
        return modifiedOn;
    }

    @Column(name = "exchange_rate")
    public Float getExchangeRate() {
        return exchangeRate;
    }

    @Column(name = "equivalent_amount")
    public Float getEquivalentAmount() {
        return equivalentAmount;
    }

    @Column(name = "local_amount")
    public Float getLocalAmount() {
        return localAmount;
    }

    @Column(name = "foreign_amount")
    public Float getForeignAmount() {
        return foreignAmount;
    }

    @Column(name = "old_id")
    public Long getOldId() {
        return oldId;
    }

    @Column(name = "notes", length = 250)
    public String getNotes() {
        return notes;
    }

    @ManyToOne
    @JoinColumn(name = "resolution_id")
    public Resolution getResolution() {
        return resolution;
    }

    @ManyToOne
    @JoinColumn(name = "work_type_id")
    public WorkType getWorkType() {
        return workType;
    }

    @Column(name = "old_status")
    public Long getOldStatus() {
        return oldStatus;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setTransDate(Date transDate) {
        this.transDate = transDate;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    public void setSubtotal(Float subtotal) {
        this.subtotal = subtotal;
    }

    public void setTax(Float tax) {
        this.tax = tax;
    }

    public void setTotalCost(Float totalCost) {
        this.totalCost = totalCost;
    }

    public void setDeliveryTime(DeliveryTime deliveryTime) {
        this.deliveryTime = deliveryTime;
    }

    public void setPaymentMethod(PaymentMethod paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    public void setGuarantee(Guarantee guarantee) {
        this.guarantee = guarantee;
    }

    public void setStatus(QuotationStatus status) {
        this.status = status;
    }

    public void setDeliveryStatus(QuotationStatus deliveryStatus) {
        this.deliveryStatus = deliveryStatus;
    }

    public void setExchangeCurrency(Currency exchangeCurrency) {
        this.exchangeCurrency = exchangeCurrency;
    }

    public void setDetails(List<QuotationDetail> details) {
        this.details = details;
    }

    public void setExchangeRate(Float exchangeRate) {
        this.exchangeRate = exchangeRate;
    }

    public void setEquivalentAmount(Float equivalentAmount) {
        this.equivalentAmount = equivalentAmount;
    }

    public void setLocalAmount(Float localAmount) {
        this.localAmount = localAmount;
    }

    public void setForeignAmount(Float foreignAmount) {
        this.foreignAmount = foreignAmount;
    }

    public void setOldId(Long oldId) {
        this.oldId = oldId;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public void setResolution(Resolution resolution) {
        this.resolution = resolution;
    }

    public void setWorkType(WorkType workType) {
        this.workType = workType;
    }

    public void setOldStatus(Long oldStatus) {
        this.oldStatus = oldStatus;
    }

    @Override
    public String toString() {
        return description;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if (!(other instanceof Quotation)) {
            return false;
        }
        Quotation _i = (Quotation) other;
        return id.equals(_i.id);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    public void addDetail(QuotationDetail detail) {
        detail.setQuotation(this);
        details.add(detail);
    }

    public void deleteDetail(QuotationDetail detail) {
        details.remove(detail);
        detail.setProduct(null);
        detail.setQuotation(null);
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public void setModifiedBy(User modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public void setModifiedOn(Date modifiedOn) {
        this.modifiedOn = modifiedOn;
    }
}
